package cn.com.gridinfo.sooc.core.course.classes.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.core.course.classes.bean.CourseInfo;


public class CourseListAdapter extends UltimateViewAdapter<CourseListAdapter.ViewHolder> {

    public List<CourseInfo> courseList;
    private ViewHolder viewHolder;
    Context context;
    private boolean isEvaluation = false;//是否需要评价 默认不需要
    private boolean isPay = true;//是否显示收费 默认显示

    public CourseListAdapter(List<CourseInfo> list, Context context) {
        this.courseList = list;
        this.context = context;
        isPay = Arad.preferences.getBoolean(LocalMark.IS_PAY.getName());
    }

    /**
     * 是否需要评价
     *
     * @param list         课程列表
     * @param context
     * @param isEvaluation 是否需要评价
     */
    public CourseListAdapter(List<CourseInfo> list, Context context, boolean isEvaluation) {
        this.courseList = list;
        this.context = context;
        this.isEvaluation = isEvaluation;
        isPay = Arad.preferences.getBoolean(LocalMark.IS_PAY.getName());
    }


    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view;
        //平台是否收费
        if (!isPay) {
//        是否需要评价
            if (isEvaluation) {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_index_list_item, parent, false);
            } else {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_list_item, parent, false);
            }
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_list_item_pay, parent, false);
        }
        viewHolder = new ViewHolder(view, true);
        view.setOnClickListener(viewHolder);
        return viewHolder;
    }

    /**
     * 点击事件接口
     */
    public interface OnItemClickListener {
        void OnItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {

        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (position < getItemCount() && (customHeaderView != null ? position <= courseList.size() : position < courseList.size())
                && (customHeaderView != null ? position > 0 : true)) {

            CourseInfo t = courseList.get(position);
            Log.d("数据绑定.." + t.getKcmc());
            holder.course_name.setText(t.getKcmc());
            holder.course_teacher.setText(t.getKcjs());
            holder.course_studynum.setText(t.getKcrs() + "人学习");
            Picasso.with(context).load(t.getKcfm()).resize(context.getResources().getDimensionPixelSize(R.dimen.course_img_w), context.getResources().getDimensionPixelSize(R.dimen.course_img_h)).placeholder(R.mipmap.course_placeholder)
                    .error(R.mipmap.course_placeholder).into(holder.course_img);

            if (isEvaluation) {
                holder.course_ratingBar.setRating(Float.parseFloat(t.getKcpj()));
            }

            if (isPay) {
                if (t.getIs_charge().equals("1")) {
                    holder.price.setText("￥"+t.getPrice());
                    holder.price.setTextColor(context.getResources().getColor(R.color.red_hint));
                } else {
                    holder.price.setText("免费");
                    holder.price.setTextColor(context.getResources().getColor(R.color.green));
                }
            }
        }
    }

    public void loadmore(List<CourseInfo> list) {
        courseList.addAll(list);
        notifyDataSetChanged();
    }

    public void refresh(List<CourseInfo> list) {
        clear(courseList);
        courseList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getAdapterItemCount() {
        return courseList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    public class ViewHolder extends UltimateRecyclerviewViewHolder implements View.OnClickListener {
        TextView course_name, course_teacher, course_studynum, price;//课程名称,老师,学习人数,价钱
        ImageView course_img;//课程图片
        RatingBar course_ratingBar;//课程评价


        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
//               课程名称
                course_name = (TextView) itemView.findViewById(R.id.course_name);
//               课程老师
                course_teacher = (TextView) itemView.findViewById(R.id.course_teacher);
//               课程学习人数
                course_studynum = (TextView) itemView.findViewById(R.id.course_study_num);
//               课程封面
                course_img = (ImageView) itemView.findViewById(R.id.course_img);

                //有评价的话
                if (isEvaluation) {
                    course_ratingBar = (RatingBar) itemView.findViewById(R.id.course_ratingbar);
                    course_ratingBar.setIsIndicator(true);
                }

                price = (TextView) itemView.findViewById(R.id.price);
            }
        }


        @Override
        public void onClick(View v) {
            mOnItemClickLitener.OnItemClick(v, getAdapterPosition());
        }
    }

}
